package com.felix.servlet;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.g3net.tool.FileUtils;

public class Updata extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String keywordpath = "/data/keyword/";
	public static final String adpath = "/data/AD/";

	// @Override
	public void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		java.io.OutputStream out = response.getOutputStream();
		final DataOutputStream dos2 = new DataOutputStream(
				new BufferedOutputStream(out));

		String cmd = request.getParameter("cmd");
		if (cmd == null) {
			dos2.writeByte(-1);
			return;
		} else {
			try {
				if (cmd.equalsIgnoreCase("keyword")) {
					String keywordFile = request.getParameter("name");
					File file = new File(keywordpath);
					if (!file.exists()){
						dos2.writeByte(-1);
						return;
					}

					File[] files = file.listFiles();
					if (files == null || files.length != 1) {
						dos2.writeByte(-1);
						return;
					}

					String servicePath = files[0].getName();
					if (keywordFile == null) {// 没有广告图片
						dos2.writeByte(1);
						dos2.writeUTF(servicePath);
						byte[] b = FileUtils.readBytes(files[0]);
						dos2.writeInt(b.length);
						dos2.write(b);
					} else {
						if (servicePath.equalsIgnoreCase(keywordFile)) {// 图片文件一样，不更新
							dos2.writeByte(-1);
							return;
						} else {
							dos2.writeByte(1);
							dos2.writeUTF(servicePath);
							byte[] b = FileUtils.readBytes(files[0]);
							dos2.writeInt(b.length);
							dos2.write(b);
						}
					}
				} else if (cmd.equalsIgnoreCase("adimage")) {
					String keywordFile = request.getParameter("name");
					File file = new File(adpath);
					if (!file.exists()){
						dos2.writeByte(-1);
						return;
					}

					File[] files = file.listFiles();
					if (files == null || files.length == 0) {
						dos2.writeByte(-4);//删除图片
						return;
					}

					String servicePath = files[0].getName();
					if (keywordFile == null) {// 没有广告图片
						dos2.writeByte(1);
						dos2.writeUTF(servicePath);
						byte[] b = FileUtils.readBytes(files[0]);
						dos2.writeInt(b.length);
						dos2.write(b);
					} else {
						if (servicePath.equalsIgnoreCase(keywordFile)) {// 图片文件一样，不更新
							dos2.writeByte(-1);
							return;
						} else {
							dos2.writeByte(1);
							dos2.writeUTF(servicePath);
							byte[] b = FileUtils.readBytes(files[0]);
							dos2.writeInt(b.length);
							dos2.write(b);
						}
					}
				}
			} catch (Exception e) {
				System.out.println("wangguang-------------" + e.getMessage());
				e.printStackTrace();
			}finally{
				dos2.flush();
				out.close();
				dos2.close();
			}
		}

	}

	public void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		this.doGet(request, response);
	}

}
